//
//  ChooseTimeController01.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/18.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "ChooseTimeController01.h"

@interface ChooseTimeController01 ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *ChooseTimeView01;



@property (strong, nonatomic) NSMutableArray * YearArr;
@property (strong, nonatomic) NSArray * MonthArr;
@property (strong, nonatomic) NSString * TimeStr;

@end

@implementation ChooseTimeController01

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.ChooseTimeView01.delegate = self;
    self.ChooseTimeView01.dataSource = self;
    
    for (int i = 0; i < 20; i ++) {
        NSString * yearStr = [NSString stringWithFormat:@"%d",2018+i];
        [self.YearArr addObject:yearStr];
    }
    
    self.MonthArr = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"];
    
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component ==1) {
        return self.YearArr.count;
    }else{
        return self.MonthArr.count;
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 1) {
        return [NSString stringWithFormat:@"%@",self.YearArr[row]];
    }else{
        return [NSString stringWithFormat:@"%@",self.MonthArr[row]];
    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
};

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    
    
    NSString * date = [NSString stringWithFormat:@"%@-%@",self.YearArr[[self.ChooseTimeView01 selectedRowInComponent:1]],self.MonthArr[[self.ChooseTimeView01 selectedRowInComponent:0]]];
    NSLog(@"选中的时间%@",date);
    
    NSString * dataStr = [NSString stringWithFormat:@"%@%ld",self.MonthArr[[self.ChooseTimeView01 selectedRowInComponent:0]],[self.ChooseTimeView01 selectedRowInComponent:1]+18];
    self.TimeStr = dataStr;
}
- (IBAction)ClickOnBtnToDismissChooseTimeView01:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)ClickOnBtnToChooseTimeofView01:(id)sender {
    if (!self.TimeStr) {
        self.TimeStr = @"1801";
    }
    if (self.TimeBack) {
        self.TimeBack(self.TimeStr);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSMutableArray *)YearArr{
    if (!_YearArr) {
        _YearArr = [NSMutableArray array];
    }
    return _YearArr;
}

- (NSMutableArray *)MonthArr{
    if (!_MonthArr) {
        _MonthArr = [NSMutableArray array];
    }
    return _MonthArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
